package top.mcwebsite.playandroid.business.settings.impl.vm

import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import top.mcwebsite.playandroid.common.api.AppService
import top.mcwebsite.playandroid.common.user.api.IUserService
import top.mcwebsite.playandroid.lib.common.ext.isNightModeActiveEx
import top.mcwebsite.playandroid.lib.mvi.base.BaseViewModel
import top.mcwebsite.playandroid.lib.servicemanager.core.ServiceManager
import top.mcwebsite.playandroid.strings.Str

class SettingsViewModel : BaseViewModel<SettingsIntent, SettingsUiState, SettingsUiEffect>(
    defaultState = SettingsUiState()
) {
    override fun handleIntent(intent: SettingsIntent) {
        when (intent) {
            is SettingsIntent.ClickBackIntent -> {
                sendUiEffect(SettingsUiEffect.NavigationBackUiEffect)
            }
            is SettingsIntent.ClickLogoutIntent -> {
                setUiState { copy(isShowLogoutDialog = true) }
            }
            is SettingsIntent.LogoutCancelIntent -> {
                setUiState { copy(isShowLogoutDialog = false) }
            }
            is SettingsIntent.LogoutConfirmIntent -> {
                logout()
            }
            is SettingsIntent.ToggleDarkModeIntent -> {
                toggleDarkMode()
            }
            is SettingsIntent.ToggleFollowSystemThemeIntent -> {
                toggleFollowSystemTheme()
            }
            is SettingsIntent.ClickPrivacyIntent -> {
                sendUiEffect(SettingsUiEffect.NavigatePrivacyPageUiEffect)
            }
            is SettingsIntent.ClickAboutUsIntent -> {
            }
        }
    }

    private fun toggleDarkMode() {
        if (uiState.isFollowSystemTheme) {
            val tips = ServiceManager.getService<AppService>().getApplication().getString(Str.follow_system_cancel)
            sendUiEffect(SettingsUiEffect.ShowSnackMessageUiEffect(tips))
            ServiceManager.getService<AppService>().followSystemTheme(false)
        }
        ServiceManager.getService<AppService>().darkMode(!uiState.isDarkMode)
        setUiState {
            copy(isDarkMode = !this.isDarkMode, isFollowSystemTheme = false)
        }
    }

    private fun toggleFollowSystemTheme() {
        ServiceManager.getService<AppService>().followSystemTheme(!uiState.isFollowSystemTheme)
        var darkMode = uiState.isDarkMode
        if (!uiState.isFollowSystemTheme) {
            // 获取系统是否是暗色模式
            val systemIsDarkMode = ServiceManager.getService<AppService>().getApplication()
                .applicationContext.resources.configuration.isNightModeActiveEx()
            ServiceManager.getService<AppService>().darkMode(systemIsDarkMode)
            darkMode = systemIsDarkMode
        }
        setUiState {
            copy(isFollowSystemTheme = !this.isFollowSystemTheme, isDarkMode = darkMode)
        }
    }

    private fun logout() {
        viewModelScope.launch {
            setUiState { copy(isShowLogoutDialog = false) }
            ServiceManager.getService<IUserService>().logout()
        }
    }
}